package com.su.dolphin.view.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.su.dolphin.common.interfaces.IUITemplete;
import com.su.dolphin.utils.LogUtil;

public abstract class BaseFragment extends Fragment implements IUITemplete, OnClickListener
{
    protected FragmentActivity mFragmentActivity;
    protected View mContentView;
    public final Bundle mFragmentBundle = new Bundle();
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int layoutId = onSetContentView();
        View view = getFragmentView(layoutId, inflater, container);
        if (view == null) {
            LogUtil.i("oh no,  you did not set content view");
            return new View(getActivity());
        }
        return view;
    }
    
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentActivity = getActivity();
        if (null != getArguments())
            mFragmentBundle.putAll(getArguments());
    }
    
    public void onDestroy() {
        mFragmentBundle.clear();
        super.onDestroy();
    }
    
    public Bundle getBundle() {
        return mFragmentBundle;
    }
    
    /**
     * 在Fragment展示给用户的时候, 更新Fragment的UI 这个函数的调用时机 1. Fragment已经在内存中,
     * 使用FragmentUtil界面切换后使用bundle(如果有)更新这个界面 2. Fragment已经在内存中,
     * 使用PeelFragmentStatePagerAdapter界面切换后使用bundle(如果有)更新这个界面 3. Fragment不在内存中,
     * Fragment 重新显示, 崩溃现场恢复, 如果有以前缓存的数据(mFragmentBundle != null)
     * 调用onActivityCreated 时候会执行update 4. Fragment不在内存中, Fragment 重新显示,
     * 如果有传来的数据的数据(mFragmentBundle != null) 调用onActivityCreated 时候会执行update
     * 
     * @param bundle
     */
    public void update(final Bundle bundle) {
        onUpdataActionBar(bundle);
        if (null == bundle)
            this.mFragmentBundle.clear();
        else
            this.mFragmentBundle.putAll(bundle);
    }
    
    /**
     * 在Fragment展示给用户的时候更新Fragment对应的ActionBar 从经验来看, Fragment作为页面的时候,
     * Actionbar其实很难管理, 建议做成 自定义Titlebar的形式.
     */
    public void onUpdataActionBar(Bundle bundle) {
        
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putAll(mFragmentBundle);
        super.onSaveInstanceState(outState);
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContentView = getView();
        initView();
        setListener();
        initData();
        if (savedInstanceState != null) {
            mFragmentBundle.putAll(savedInstanceState);
            update(savedInstanceState);
        }
        else if (getArguments() != null) {
            mFragmentBundle.putAll(getArguments());
            update(getArguments());
        }
        else {
            //首次启动更新不能执行update 所有在这里更新actionbar
            onUpdataActionBar(mFragmentBundle);
        }
        
    }
    
    protected abstract int onSetContentView();
    
    @Override
    public void setListener() {
        
    }
    
    @Override
    public void onClick(View v) {
        
    }
    
    protected View getFragmentView(int layoutId, LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(layoutId, container, false);
        return view;
    }
}
